﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>机构系统任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/lobsteruiframe/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/lobsteruiframe/layuiadmin/style/admin.css" rel="stylesheet" />
    <script src="/lobsteruiframe/layuiadmin/layui/layui.js"></script>
    <script src="/jslib/jquery-3.4.1.min.js"></script>
    <script src="/jslib/lobster.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <form class="layui-form">
            <div class="layui-card">
                <div class="layui-card-header layui-form layuiadmin-card-header-auto">
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">医疗机构：</label>
                            <div class="layui-input-block">
                                <select name="workId" id="workId" lay-filter="workId"></select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">选择应用：</label>
                            <div class="layui-input-block">
                                <select name="appId" id="appId" lay-filter="appId"></select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">任务状态：</label>
                            <div class="layui-input-block">
                                <select id="JobStatus" name="JobStatus" lay-filter="cmbJobStatus">
                                    <option value="-99" selected>全部</option>
                                    <option value="-1">未启用</option>
                                    <option value="0">暂停</option>
                                    <option value="1">正常</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称/编码：</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" id="txtJobName" name="txtJobName" placeholder="请输入任务名称/编码" />
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="query-submit" id="btnQuery">
                                <i class="layui-icon layuiadmin-button-btn layui-icon-search"></i>查询
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    
                    <table class="layui-table" id="gridJobInfo" lay-filter="gridJobInfo"></table>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

<script type="text/javascript">
    lobsterlayui.main(['index', 'layer', 'form', 'element', 'table'], {
        data: {
            $layui: null,
            right: null,
            tablejob:null,
        },
        pageload: function (right) {
            this.data.right = right;
            this.loadselectworker(() => {
                this.loadselectapp(() => {
                    this.data.tablejob=this.InitDataGrid();
                })
            })
            this.initevent();
        },
        initevent() {
            var self = this;
            // 点击查询按钮查询消息类型
            layui.form.on("submit(query-submit)", function (obj) {
                var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                self.data.tablejob.reload({
                    where: {
                        jobStatus: $("#JobStatus").val(),
                        jobName: $("#txtJobName").val(),
                        workId: $("#workId").val(),
                        appId: $("#appId").val()
                    },
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    done: function () {
                        layer.close(load);
                    }
                });
                return false;
            });

            // 监听行工具事件
            layui.table.on("tool(gridJobInfo)", function (obj) {
                var data = obj.data;
                var tr = obj.tr;
                if (obj.event === "disable-submit" || obj.event === "enable-submit") {
                    // 启用停用消息类型
                    var confirm_msg = "";
                    var status = 0;
                    if (data.JobStatus == 1) {
                        confirm_msg = "确定要暂停任务吗?";
                        status = 0;
                    }
                    else {
                        confirm_msg = "确定要开启任务吗?";
                        status = 1;
                    }
                    if (data.DelFlag == 1) {
                        layer.msg('已删除的需要先启用才能进行' + (data.JobStatus == 1 ? '暂停任务' : '开启任务'), { icon: 2, time: 1500, shade: [0.3, '#393D49'] });
                        return false;
                    }

                    layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                        function (index) {

                            lobsterlayui.GET('/admin/v1/workerjob/FlagJobStatus',{jobId: data.JobId,jobStatus: status}).then(retdata=>{
                                
                                layer.closeAll();
                                tipMsg = "";
                                if (data.JobStatus == 1) {
                                    tipMsg = "暂停";
                                }
                                else {
                                    tipMsg = "开启";
                                }

                                layer.msg(tipMsg + "任务成功");
                                
                                self.data.tablejob.reload();// 数据刷新
                                    
                            })
                        });
                }
                else if (obj.event === "exec-submit") {
                    if (data.DelFlag == 1) {
                        layer.msg('已删除的需要先启用才能执行', { icon: 2, time: 1500, shade: [0.3, '#393D49'] });
                        return false;
                    }
                    layer.confirm("确定要立即执行吗?", { icon: 3, title: "提示" },
                        function (index) {
                            lobsterlayui.GET('/job/v1/job/ExecJob',{jobId: data.JobId}).then(retdata=>{
                                layer.closeAll();
                                layer.msg("执行成功");
                                self.data.tablejob.reload();
                            }).catch(res=>{
                                layer.closeAll();
                                layer.msg(res.msg);
                            })
                        });
                }
                else if (obj.event === "setup-submit") {  //启用任务
                    layer.confirm("确定要启用吗?", { icon: 3, title: "提示" },
                        function (index) {
                            lobsterlayui.GET('/admin/v1/workerjob/SetupJob',{jobId:data.JobId,workId:$("#workId").val()}).then(retdata=>{
                                layer.msg("启用成功");
                                layer.closeAll();
                                self.data.tablejob.reload();
                            })
                        });
                }
                else if (obj.event === "record") {
                    // 查看任务执行的历史记录
                    var index = layer.open({
                        type: 1,
                        title: data.JobName + '("' + data.CronExp + '")' + '-执行记录',
                        content: $('#dlg_JobRecordForm').html(),
                        area: ["600px", "550px"],
                        success: function (layero, index) {
                            setTimeout(function () {
                                //初始化请求方式下拉框
                                self.InitRecord(data, index);
                            }, 300);
                        },
                        btn2: function (index, layero) {
                            tr.removeAttr("class");
                        },
                        cancel: function () {
                            tr.removeAttr("class");
                        }
                    });

                }
            });
        },
        loadselectworker(callback){
            //获取机构数据
            lobsterlayui.GET('/admin/v1/worker/GetSelectWorkerDataByUser').then(data => {
                //医疗机构
                var workers = data["Workers"];
                $("#workId").html("");
                $.each(workers, function (i, item) {
                    $("#workId").append("<option value=\"" + item.WorkId + "\">" + item.WorkName + "</option>");
                });

                $('#workId').val(workers[0].WorkId);//默认选择当前登录机构
                //重新渲染
                layui.form.render("select");

                if (callback) {
                    callback();
                }
            });

            //监听机构下拉框
            layui.form.on('select(workId)', function () {
                if (callback) {
                    callback();
                }
            });
        },
        //加载应用
        loadselectapp : function (callback) {

            lobsterlayui.GET('/admin/v1/worker/GetSimpleAppData',{workid:$('#workId').val()}).then(retdata=>{
                 //应用列表
                 var appdata = retdata["appdata"];
                    $("#appId").html("");
                    $.each(appdata, function (i, item) {
                        $("#appId").append("<option value=\"" + item.AppId + "\">" + item.AppName + "</option>");
                    });
                    //重新渲染
                    layui.form.render("select");

                    if (callback) {
                        callback();
                    }
            })

            //监听应用下拉框
            layui.form.on('select(appId)', function () {
                if (callback) {
                    callback();
                }
            });
        },
        InitDataGrid : function () {
            return layui.table.render({
                elem: "#gridJobInfo",
                headers:lobsterlayui.GetHeaderData(),
                where: {
                    jobStatus: $("#JobStatus").val(),
                    jobName: $("#txtJobName").val(),
                    workId: $("#workId").val(),
                    appId: $("#appId").val()
                },
                height: "full-140",
                cellMinWidth: 80, //全局定义常规单元格的最小宽度
                //toolbar: "#barJobInfo",
                //defaultToolbar: [],
                url: "/admin/v1/workerjob/GetJobList", //数据接口
                page: true, //开启分页
                loading: true,
                cols: [[ //表头
                    { field: "JobName", width: "15%", title: "任务名称" },
                    { field: "JobCode", width: "15%", title: "任务编码" },
                    { field: "CronExp", width: "10%", title: "Cron" },
                    { field: 'LastExecDate', width: '15%', title: "最后执行时间" },
                    { field: "CreateDate", width: "15%", title: "创建时间" },
                    { field: "JobStatus", width: "6%", title: "状态", templet: "#barRowJobStates" },
                    { field: "JobDesc", width: "25%", title: "描述" },
                    { field: "ReqApiUrl", width: "20%", title: "ApiUrl" },
                    { field: "ReqMethod", width: "8%", title: "请求方式" },
                    { title: "操作", width: "18%", align: 'center', toolbar: "#barRowOperate", fixed: 'right' },
                    //{ title: "执行记录", width: "8%", align: 'center', toolbar: "#barRowRecord", fixed: 'right' }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.dt
                    }
                }
            });
        },
        InitRecord(data){
            layui.table.render({
                elem: '#gridJobRecord',
                headers:lobsterlayui.GetHeaderData(),
                height: "400",
                url: '/admin/v1/workerjob/GetJobRecordList', //数据接口
                where: {
                    jobId: data.JobId
                },
                page: true, //开启分页
                cols: [[ //表头
                    { field: 'JerStartTime', title: '执行开始时间', width: "30%", sort: true },
                    { field: 'JerEndTime', title: '执行结束时间', width: "30%" },
                    { field: 'JerResult', title: 'Api返回结果', width: "60%" },
                    { field: 'JerRemark', title: '备注', width: "40%" }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.dt
                    }
                }
            });

        }
    })
</script>

<script type="text/html" id="barRowOperate">
    
    {{# if(d.JobStatus==1){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="disable-submit"><i class="layui-icon layui-extend-stop"></i>暂停</a>
    {{# } else if(d.JobStatus==-1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="setup-submit"><i class="layui-icon layui-icon-add"></i>启用</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable-submit"><i class="layui-icon layui-icon-ok"></i>恢复</a>
    {{# } }}

    {{# if(d.JobStatus==1){ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="exec-submit"><i class="layui-icon layui-icon-play"></i>执行</a>
    <a class="layui-btn layui-btn-xs" lay-event="record"><i class="layui-icon layui-icon-time"></i>查看记录</a>
    {{# } else { }}
    {{# } }}

</script>

<script type="text/html" id="barRowJobStates">
    {{# if(d.JobStatus==0){ }}
    <span style="color:red">暂停</span>
    {{# } else if(d.JobStatus==-1) { }}
    <span style="color:black">未启用</span>
    {{# } else { }}
    <span style="color:#5FB878">正常</span>
    {{# } }}
</script>

<script type="text/html" id="dlg_JobRecordForm">
    <div class="layui-fluid box">
        <form class="layui-form" lay-filter="formJobRecord" id="formJobRecord" style="padding: 0px 0px 0 0;">
            <div class="layui-card">
                <div class="layui-card-body box">
                    <div class="layui-form-item active">
   
                        <table id="gridJobRecord" lay-filter="gridJobRecord"></table>
                    </div>
                    
                </div>
            </div>
        </form>
   </div>
</script>